/**
 * Created by Administrator on 2017/8/24.
 */

let net = require('net');
let variable = require('../data/variable.js');
let gameData = require('../util/gameData.js');
let log = require('../util/log.js');
let util = require('../util/util.js');
let msg = require('../util/msg.js');

let rpc = module.exports;

rpc.dc = {
    link: {},
    cbList: {},
    connect: function (self, cb) {
        rpc.dc.link = {};
        connect(self, 'DC', 'dc', variable.dc.local, variable.dc.port, cb);
    },
    send: function (action, data, cb) {
        let uuid = null;
        if (cb) {
            uuid = util.uuid();
            rpc.dc.cbList[uuid] = cb;
        }
        msg.carry(rpc.dc.link[variable.dc.local + ':' + variable.dc.port], uuid, action, -1, data);
    }
};
rpc.match = {
    link: {},
    cbList: {},
    connect: function (self, cb) {
        rpc.match.link = {};
        for (let i in variable.match) {
            connect(self, '排队', 'match', variable.match[i].local, variable.match[i].port);
        }
    },
    send: function (action, data, cb) {
        let uuid = null;
        if (cb) {
            uuid = util.uuid();
            rpc.match.cbList[uuid] = cb;
        }
        msg.carry(rpc.match.link[variable.match[0].local + ':' + variable.match[0].port], uuid, action, -1, data);
    }
};
rpc.game = {
    link: {},
    cbList: {},
    connect: function (self) {
        rpc.game.link = {};
        for (let i in variable.game) {
            connect(self, '游戏', 'game', variable.game[i].local, variable.game[i].port);
        }
    },
    send: function (action, data, cb) {
        let uuid = null;
        if (cb) {
            uuid = util.uuid();
            rpc.game.cbList[uuid] = cb;
        }
        let game;
        switch (action) {
            case 'createRoom':
                game = util.getRandGameServer('match');
                break;
            case 'happyRoom':
                game = util.getRandGameServer('happy');
                break;
            case 'customRoom':
                game = util.getRandGameServer('custom');
                break;
            case 'arenaRoom':
            case 'rpcInArena':
                game = util.getRandGameServer('arena');
                break;
            case 'followRoom':
                game = util.getGameIdServer(data.gameId);
                break;
        }
        msg.carry(rpc.game.link[game.local + ':' + game.port], uuid, action, -1, data);
    }
};


function connect(self, name, type, local, port, cb) {
    let key = local + ':' + port;
    rpc[type].link[key] = net.connect({local: local, port: port}, function () {
        log.p(self + ' to ' + name + ' ' + key + '连接成功');
        if (cb) {
            cb();
        }
    });
    rpc[type].link[key].on("end", function () {
        log.e(self + ' to ' + name + ' ' + key + '断开');
    });
    rpc[type].link[key].on("error", function () {
        log.e(self + ' to ' + name + ' ' + key + '错误，尝试重连');
        setTimeout(function () {
            delete rpc[type].link[key];
            connect(self, name, type, local, port, cb)
        }, 1000);
    });
    rpc[type].link[key].on("data", function (message) {
        let list = util.messageToArr(message, util.getServerInfo('id'));
        foreachPush(list, type);
    });
}

function foreachPush(list, type) {
    for (let i in list) {
        if (!list[i]) {
            continue;
        }
        let message = list[i];
        console.log('foreachPush', util.getServerInfo('id'), message);
        gameData.getMessage(message);
        if (!rpc[type].cbList[message.uuid]) {
            continue;
        }
        rpc[type].cbList[message.uuid](message.obj);
        delete rpc[type].cbList[message.uuid];
    }
}